package com.xisoft.devise.service.impl;

import com.xisoft.base.entity.BaseVO;
import com.xisoft.base.service.XfServiceImpl;
import com.xisoft.common.bean.ReflectionUtil;
import com.xisoft.common.db.SqlHelper;
import com.xisoft.common.io.StreamUtil;
import com.xisoft.common.pubEnv.JsonUtil;
import com.xisoft.common.pubEnv.PubEnvUtil;
import com.xisoft.common.pubEnv.StringUtil;
import com.xisoft.devise.entity.salary.SalaryDataEntity;
import com.xisoft.devise.mapper.SalaryDataMapper;
import com.xisoft.devise.service.ISalaryDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * *
 * Created with Description:
 *
 * @version 1.0 <br>
 * @packagename: com.xisoft.devise.service.impl<br>
 * @author: LiBencheng <br>
 * @computer: licheng <br>
 * @date: Created on 2022/8/12 14:58 <br>
 */
@Service ("SalaryDataImpl")
@Transactional
public class SalaryDataImpl extends XfServiceImpl implements ISalaryDataService
{

    @Autowired
    private SalaryDataMapper mapper;

    @Override public int delSalaryData(String json) throws Exception
    {
        Map< String,Object > toMap = JsonUtil.toMap(json);
        BaseVO baseEntity = ReflectionUtil.newInstance(StringUtil.toString(toMap.get("classpatch")));
        List< String > ids = Arrays.asList(StringUtil.toString(toMap.get("id")).split(PubEnvUtil.COMMA));
        String selectSQL = SqlHelper.getSelectSQL(baseEntity.getTableName()
                , new String[]{baseEntity.getPrimaryKey()}
                , SqlHelper.getPkInSQLByCol(baseEntity.getPrimaryKey(), ids), null);
        System.out.println(selectSQL);

        Map< String,String > childMap = baseEntity.getChildEntity();
        for (String tableCode : childMap.keySet())
        {
            BaseVO childEntity = ReflectionUtil.newInstance(childMap.get(tableCode));
            System.out.println(childEntity.getChildEntity());
            String deleteSQL = SqlHelper.getDeleteSQL(childEntity.getTableName()
                    , baseEntity.getPrimaryKey() + String.format(SqlHelper.in, selectSQL));
            System.out.println(deleteSQL);
        }

        String deleteSQL = SqlHelper.getDeleteSQL(baseEntity.getTableName(),
                                                  SqlHelper.getPkInSQLByCol(baseEntity.getPrimaryKey(), ids));
        System.out.println(deleteSQL);

        return 0;
    }

    @Override public int updSalaryData(Object obj) throws Exception
    {
        String filePath = this.getClass().getResource("/config/salarydata.json").getPath();
        String str = new String(StreamUtil.readFileToByteArray(filePath), PubEnvUtil.UTF_8);
        List< Map< String,Object > > jsonObj = JsonUtil.toList(str);
        SalaryDataEntity fatherVO = (SalaryDataEntity) getBhelp().parseVO(jsonObj, SalaryDataEntity.class);
        List aggEntity = getBeanAggEntity(SalaryDataEntity.class.getName(), jsonObj);

        return 0;
    }


    @Override public Object getSalaryDataAgg(String id) throws Exception
    {
        String selectSQL = SqlHelper.getSelectSQL(SalaryDataEntity.TABLE_NAME, null, "1=1", "");
        selectSQL = "SELECT * FROM HR_SALARY_DATA WHERE SALARY_DATA_ID IN('yydsbc1d73776a9040289275eed7ed6113a0'," +
                "'yyds046023c2e0e64724858a9b4db6e15e06')";
        return getBeanAggEntity(SalaryDataEntity.class.getName(), selectSQL);
    }


}
